import { upload_file_staticNode } from './cla_upload_file_staticNode';
import { upload_file_eventBind } from './cla_upload_file_eventBind';
import { spacename_upload_file } from '../DataDefine/sourceData';
import { listenEvent_mana } from '../../../../global_comps/Classes/listenEvent_mana/Classes/cla_listenEvent_mana';
import {
  get_keyVal_node_info,
  remove_keyVal_node_info,
  set_keyVal_node_info,
} from '../../../../global_comps/uni_comps/keyVal_node/keyVal_node';
import { remove_class } from '../../../../global_comps/Func/remove_class';
import { css_cursor_pointer, css_disnone } from '../../../../global_comps/Css';
import { settimeNth } from '../../../../../global/Func/settimeNth';
import { remove_dom_func_promise } from '../../../../global_comps/Func/remove_dom';
import { inject_dom_func_promise } from '../../../../global_comps/Func/inject_dom';
import { certain_dom_func_promise } from '../../../../global_comps/Func/certain_dom';
import { reback_dom_func_promise } from '../../../../global_comps/Func/reback_dom';
import { sleep } from '../../../../../global/Func/sleep';
import { get_upload_operate_obj } from '../../../upload';
import { bind_file_info } from '../Func/bind_file_info';

const upload_file_dataManaTag = 'upload_file_dataMana';

export class upload_file_dataMana extends upload_file_staticNode {
  get [Symbol.toStringTag]() {
    return upload_file_dataManaTag;
  }

  constructor() {
    super();
  }

  /**
   * 事件 绑定 this
   */
  eventBind = new upload_file_eventBind();

  /**
   * 事件 监听管理
   */
  eventM = new listenEvent_mana(spacename_upload_file);

  /**
   * 记录 文件 信息
   */
  async sign_file(data_file: DataTransfer | FileSystemFileHandle[]) {
    set_keyVal_node_info(this.dom_keyVal_file, data_file);

    if (Array.isArray(data_file)) {
      let file_h = data_file[0];
      let file = await file_h.getFile();

      bind_file_info(file);
    }
  }

  /**
   * 取消 文件 信息 记录
   */
  remove_file() {
    remove_keyVal_node_info(this.dom_keyVal_file);
  }

  /**
   * 元素 拖拽 进入后 禁用 拖拽 进入 启用 拖拽 出元素
   */
  freeze_drag_enter() {
    this.eventM.freeze(this.dom_body, 'dragenter');

    this.eventM.unfreeze(this.dom_body, 'dragleave');
  }

  /**
   * 元素 拖拽 出元素 禁用 拖拽 出元素 启用 拖拽 进入
   */
  freeze_drag_leave() {
    this.eventM.unfreeze(this.dom_body, 'dragenter');

    this.eventM.freeze(this.dom_body, 'dragleave');
  }

  /**
   * 禁用 点击 选择文件
   */
  freeze_body_click() {
    this.eventM.freeze(this.dom_body, 'click');
  }

  /**
   * 解禁 点击 选择文件
   */
  unfreeze_body_click() {
    settimeNth(0, () => {
      this.eventM.unfreeze(this.dom_body, 'click');
    });
  }

  /**
   * 将 上传 元素 回退到 正常 元素
   */
  async #nor_css() {
    // remove_class(this.dom_body, css_cursor_pointer);

    await certain_dom_func_promise(this.dom_placeholder_up);

    remove_class(this.dom_placeholder_up, css_disnone, true);

    await inject_dom_func_promise(this.dom_placeholder_load);

    remove_class(this.dom_placeholder_load, css_disnone);
  }

  /**
   * 将 正常 元素 特别 显示 上传 元素
   */
  async #spe_css() {
    // remove_class(this.dom_body, css_cursor_pointer, true);

    await remove_dom_func_promise(this.dom_placeholder_load);

    remove_class(this.dom_placeholder_load, css_disnone, true);

    await reback_dom_func_promise(this.dom_placeholder_up);

    remove_class(this.dom_placeholder_up, css_disnone);
  }

  async #chang_css() {
    await remove_dom_func_promise(this.dom_placeholder_load);

    remove_class(this.dom_placeholder_load, css_disnone, true);

    await sleep(10);

    await inject_dom_func_promise(this.dom_placeholder_load);
  }

  #father_upload_load() {
    if (this.father_operate_obj == undefined) {
      this.father_operate_obj = get_upload_operate_obj();
    }

    this.father_operate_obj.load_file();
  }

  #father_upload_remove() {
    if (this.father_operate_obj == undefined) {
      this.father_operate_obj = get_upload_operate_obj();
    }

    this.father_operate_obj.remove_file();
  }

  /**
   * 正常流 元素
   */
  async normal_doc() {
    this.#father_upload_load();

    remove_class(this.dom_body, css_cursor_pointer);

    await this.#nor_css();

    this.freeze_body_click();
  }

  /**
   * 特别流 元素
   */
  async special_doc() {
    this.#father_upload_remove();

    await this.#spe_css();

    remove_class(this.dom_body, css_cursor_pointer, true);

    this.unfreeze_body_click();
  }

  /**
   * 更定 文件
   */
  async change_file() {
    this.#father_upload_load();

    this.father_operate_obj?.change_file();

    this.#chang_css();
  }

  /**
   * 拖拽 文件 信息 记录
   */
  drag_file_load(event: DragEvent) {
    // 当拖拽的不是文件 拒绝 上传 操作
    if (event.dataTransfer?.files.length == 0) {
      return;
    }

    // 记录 文件信息
    this.sign_file(event.dataTransfer!);

    console.log(get_keyVal_node_info(this.dom_keyVal_file));
  }

  async click_file_load(file: FileSystemFileHandle[]) {
    console.log(await (await file[0].getFile()).text());

    // 记录 文件信息
    this.sign_file(file);

    console.log(get_keyVal_node_info(this.dom_keyVal_file));
  }
}
